home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_300 / 323_01 / parser.c < prev    next >
C/C++ Source or Header  |  1990-08-04  |  3KB  |  114 lines

  1. /*--------------------------------------------------------------------------*\
  2. | PARSER.C                                                                   |
  3. \*--------------------------------------------------------------------------*/
  4.  
  5. /*
  6.    Adventure Parser
  7.  
  8.    Need to include the adv-world definition file before including this one.
  9.    This is because the vocabulary definitions depend on the adventure-world
  10.    objects.
  11. */
  12.  
  13. #include "string.h"
  14.  
  15. #include "adv-def.h"
  16. #include "parser.h"
  17.  
  18. CmdRec cmd;           /* global command structure */
  19.  
  20. #define max(a,b) (((a) > (b)) ? (a) : (b))
  21. #define min(a,b) (((a) < (b)) ? (a) : (b))
  22.  
  23.  
  24. /*----------------------------*/
  25. /* Get the verb and the noun from a command of the form "verb noun".
  26.    If noun is omitted, a null string returned.
  27.  
  28.    Will handle leading blanks, and too long strings.
  29. */
  30. ParseCommand( char *cm, char *verb, char *noun )
  31. {
  32.   char *p,*q;
  33.  
  34.   *verb='\0'; *noun='\0';
  35.   while (*cm && *cm==' ') cm++;   /* skip leading blanks in command */
  36.  
  37.   p = strchr( cm, ' ' );          /* find space delimiting verb */
  38.   q = strchr( cm, '\0');          /* get last char */
  39.  
  40.   if (!p)   p = q;                /* if 1 word, set ptr */
  41.   strncat( verb, cm, min(p-cm, max_word_len) );
  42.  
  43.   if (*p)                         /* if >1 word */
  44.   {
  45.     while (*p && *p==' ') p++;    /* skip lead blanks */
  46.     strncat( noun, p, min(q-p, max_word_len) );
  47.   }
  48. }
  49.  
  50. /*----------------------------*/
  51. /* Make a string exactly v_sig characters long (plus null), truncating or
  52.    blank-padding if necessary
  53. */
  54. resize_word( char *s )
  55. {
  56.   int i;
  57.  
  58.   for ( i=strlen(s); i<v_sig; i++ )
  59.     *(s+i) = ' ';
  60.   *(s+v_sig)='\0';
  61. }
  62.  
  63. /*----------------------------*/
  64. /* Return a word's vocabulary #, given the vocab array and it's size
  65. */
  66. int GetWordNum( char *w,   vocab_type *voc )
  67. {
  68.   int wn,i;
  69.   v_word sh_word;
  70.  
  71.   strcpy( sh_word, w );
  72.   resize_word( sh_word );
  73.   for ( wn=0;  *voc->name;  voc++ )
  74.   {
  75.     if (!strcmp( sh_word, voc->name ))
  76.     {
  77.       wn = voc->num;
  78.       break;
  79.     }
  80.   }
  81.   return( wn );
  82. }
  83.  
  84. /*----------------------------*/
  85. /*
  86. */
  87. int GetVerbNum( char *verb )
  88. {
  89.   return( GetWordNum( verb, v_verb ));
  90. }
  91.  
  92. int GetNounNum( char *noun )
  93. {
  94.   return( GetWordNum( noun, v_noun ));
  95. }
  96.  
  97. /*----------------------------*/
  98. /* Parse the command into the verb and noun, then find out the verb & noun #'s
  99. */
  100. AnalyseCommand( CmdRec *cmd )
  101. {
  102.   ParseCommand( cmd->cm,   cmd->verb, cmd->noun );
  103.   strlwr( cmd->verb );
  104.   strlwr( cmd->noun );
  105.   cmd->vn = GetVerbNum( cmd->verb );
  106.   cmd->nn = GetNounNum( cmd->noun );
  107.  
  108.   strcpy( cmd->sh_verb, cmd->verb );
  109.   resize_word( cmd->sh_verb );
  110.   strcpy( cmd->sh_noun, cmd->noun );
  111.   resize_word( cmd->sh_noun );
  112. }
  113.  
  114.